<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.io.File"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%!
String path = "E:/temp2/downloadfile/";
%>
<%
//如果是下载逻辑
String filename = request.getParameter("filename");
String downloadurl = request.getParameter("downloadurl");
if(downloadurl!=null){

    response.reset();
	String filedisplay = filename; 
    String filedownload = path + downloadurl; 
    response.setContentType("applicaiton/x-download"); 
    response.addHeader("Content-Disposition", "attachment;filename="+new String(filedisplay.getBytes("UTF-8"),"ISO8859-1"));      
    InputStream is = null; 
    OutputStream os = null; 
    BufferedInputStream bis = null; 
    BufferedOutputStream bos = null;      
    is = new FileInputStream(new File(filedownload)); 
    bis = new BufferedInputStream(is); 
    os = response.getOutputStream(); 
    bos = new BufferedOutputStream(os);      
    byte[] b = new byte[1024]; 
    int len = 0; 
    while((len = bis.read(b)) != -1){ 
      bos.write(b,0,len); 
    }  
    os.flush();
    bis.close(); 
    bos.close(); 
    is.close();
    os.close();
    out.clear();
	return;
}
%>
<%!
/**
 * 获取路径下的文件列表
 * @param extension
 * @return
 */
public List<File> getFlist(String path){
	File f1 = new File(path);
	List<File> flist = new ArrayList<>();
	File [] farray = f1.listFiles();
	if(farray!=null){
		for(File f:farray){
			if(f.isDirectory()){
				List<File> flist_temp = getFlist(f.getPath());
				flist.addAll(flist_temp);
			}else{
				flist.add(f);
			}
		}
	}
	return flist;
};
/**
 * 获取文件类型
 * @param extension
 * @return
 */
public String getFiletype(String filename){
	if(filename.lastIndexOf(".")<0){
		return "file";
	}
	String extension = filename.substring(filename.lastIndexOf("."), filename.length());
	String image = ".png,.jpg,.jpeg,.gif,.bmp";
	String audio = ".mp3";
	String wcd = ".wcd";
	String video = ".flv,.swf,.mkv,.avi,.rm,.rmvb,.mpeg,.mpg,.ogg,.ogv,.mov,.wmv,.mp4,.webm,.wav,.mid";
	if(image.contains(extension.toLowerCase())){
		return "image";
	}else if(audio.contains(extension.toLowerCase())){
		return "audio";
	}else if(video.contains(extension.toLowerCase())){
		return "video";
	}else if(wcd.contains(extension.toLowerCase())){
		return "wcd";
	}else{
		return "file";
	}
};
%>
<%
String path11 = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path11+"/";
session.setAttribute("basePath",basePath);

path = path.replace("\\", "/");
/**所有文件列表*/
List<File> filelist = getFlist(path);
/**所有wcd文件列表*/
List<File> wcdlist = new ArrayList<>();
/**缓存图片数据一遍对比*/
Map<String,File> imgmap = new HashMap<>();
for(File file:filelist){
	String fname = file.getName();
	if(getFiletype(fname).equals("wcd")){
		wcdlist.add(file);
	}else if(getFiletype(fname).equals("image")){
		imgmap.put(fname.substring(0,fname.lastIndexOf(".")), file);
	}
}
/**最终文件列表*/
List<Map<String,String>> list = new ArrayList<>();
for(File file:wcdlist){
	String fname = file.getName();
	File imgfile = imgmap.get(fname.substring(0,fname.lastIndexOf(".")));
	if(imgfile==null){
		continue;
	}
	
	String filepath = file.getAbsolutePath();
	Map<String,String> map = new HashMap<>();
	map.put("name", fname);
	map.put("imgname", imgfile.getName());
	map.put("url", filepath.replace("\\", "/").replace(path, ""));
	map.put("imgurl", imgfile.getAbsolutePath().replace("\\", "/").replace(path, ""));
	list.add(map);
}

request.setAttribute("list", list);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件下载</title>
<style type="text/css">
table,table tr th, table tr td { border:1px solid #666666;}
table { width: 100%; min-height: 25px; line-height: 25px; text-align: center; border-collapse: collapse; padding:2px;}
.img-item{
	max-width: 60px;
	max-height: 60px;
}   
#hoveimg{
	max-width: 200px;
	max-height: 200px;
	border: 5px solid #A9C9E2;
}   
</style>
</head>
<body>
<div style="line-height: 40px;text-align: center;font-weight: bold;">请将图片和wcd文件下载到skclient同目录的<font color="red">Templates</font>下</div>
<table>
	<thead>
		<tr>
			<th>缩略图</th>
			<th>文件名称</th>
			<th width="180">操作</th>
		</tr>
	</thead>
	<tbody>
		<!--  -->
		<c:forEach items="${list }" var="file">
		<tr>
			<td><img class="img-item" src="filedown.jsp?filename=${file.imgname }&downloadurl=${file.imgurl }" onmousemove="imgover(event,'filedown.jsp?filename=${file.name }&downloadurl=${file.imgurl }')" onmouseout="imgout()"></td>
			<td>${file.name }</td>
			<td>
			<a href="javascript:download('filedown.jsp?filename=${file.name }&downloadurl=${file.url }','filedown.jsp?filename=${file.imgname }&downloadurl=${file.imgurl }');">下载</a>
			<!-- <a href="filedown.jsp?filename=${file.imgname }&downloadurl=${file.imgurl }">下载图片</a>&nbsp;|&nbsp;
			<a href="filedown.jsp?filename=${file.name }&downloadurl=${file.url }">下载</a> -->
			</td>
		</tr>
		</c:forEach>
	</tbody>
</table>
<img alt="" src="" id="hoveimg" style="display: none;position: absolute;"/>
<iframe id="url1" src="" style="display: none"></iframe>
<iframe id="url2" src="" style="display: none"></iframe>
<script type="text/javascript">
function imgover(ev,url){
	var x = ev.clientX;
	var y = ev.clientY;
	var item = document.getElementById("hoveimg");
	item.style.left= (x+10)+"px";
	item.style.top= (y+10)+"px";
	if(y+200>document.documentElement.clientHeight){
		item.style.top= (y-10-item.clientHeight)+"px";
	}
	item.src = url;
	item.style.display="block";
	
}
function imgout(url){
	var item = document.getElementById("hoveimg");
	item.style.display="none";
}
function download(url1,url2){
	document.getElementById("url1").src=""; 
	//document.getElementById("url2").src=""; 
	document.getElementById("url1").src=url1; 
	//document.getElementById("url2").src=url2; 
	//window.open(url1);
	window.open(url2);
}
</script>
</body>
</html>